package z_app_lotto2;

import javax.swing.*;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import java.awt.event.*;
import java.util.*;
import java.awt.*;

class MaxNumber extends JInternalFrame implements ActionListener {
	String fileName;
	Vector lotto;

	int selectLotto[][];
	String header[];

	JPanel topPanel;
	JLabel label;
	JTextField startNumber, lastNumber;
	JButton startButton, numberSort, cntSort;
	JTable table;
	JScrollPane scroll;
	MyTable model;

	public MaxNumber(Vector l) {
		super("발생회수별 List",true, true, true, false);
		fileName = this.getClass().getSimpleName() + ".cfg";
		
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameOpened(InternalFrameEvent e) {
				setBounds(new InterWindowAdapter(fileName).read());
			}
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				InterWindowAdapter adapter = new InterWindowAdapter(fileName);
				int x = getX();
				int y = getY();
				int w = getWidth();
				int h = getHeight();
				Rectangle rect = new Rectangle(x,y, w, h);
				adapter.save(rect);
			}
		});

		this.setVisible(true);
		
		this.lotto = l;
		selectLotto = new int[45][3];

		initHeader();
		initArray();

		initForm();
	}

	public void initArray() {
		for (int i = 0; i < 45; i++) {
			selectLotto[i][0] = i + 1;
			selectLotto[i][1] = i + 1;
			selectLotto[i][2] = 0;
		}
	}

	public void initHeader() {
		header = new String[3];
		header[0] = "순";
		header[1] = "No";
		header[2] = "횟수";
	}

	public void initForm() {
		this.setLayout(new BorderLayout());

		// 상단 서브 메뉴
		topPanel = new JPanel();
		topPanel.setBackground(new Color(255, 255, 255));
		label = new JLabel("회차");
		startNumber = new JTextField(3);
		lastNumber = new JTextField(3);

		startButton = new JButton("조회");
		startButton.addActionListener(this);

		numberSort = new JButton("번호순");
		numberSort.addActionListener(this);

		cntSort = new JButton("발생순");
		cntSort.addActionListener(this);

		topPanel.add(label);
		topPanel.add(startNumber);
		topPanel.add(lastNumber);
		topPanel.add(startButton);
		topPanel.add(numberSort);
		topPanel.add(cntSort);

		// button disable
		numberSort.setEnabled(false);
		cntSort.setEnabled(false);

		this.add(topPanel, BorderLayout.NORTH);
		this.updateUI();

	}

	public void select() {
		initArray(); // 배열값 초기화

		int start, last, x, y, temp;
		int len;

		Vector vector;

		if (this.startNumber.getText().equals(""))
			start = 1;
		else
			start = Integer.parseInt(this.startNumber.getText());

		if (this.lastNumber.getText().equals(""))
			last = lotto.size();
		else
			last = Integer.parseInt(this.lastNumber.getText());
		if (start > last) {
			temp = start;
			start = last;
			last = temp;
		}

		len = lotto.size();
		for (int i = 0; i < len; i++) {
			vector = (Vector) lotto.get(i);
			x = Integer.parseInt((String) vector.get(0));
			if (x < start || x > last)
				continue;

			for (int j = 1; j < 8; j++) {
				y = Integer.parseInt((String) vector.get(j)) - 1;
				selectLotto[y][2]++;

			}

		}

		table = new JTable();
		scroll = new JScrollPane(table);
		model = new MyTable(header, selectLotto);
		table.setModel(model);
		this.add(scroll);

		this.numberSort.setEnabled(true);
		this.cntSort.setEnabled(true);

		this.updateUI();
	}

	public void numberSort() {
		int temp, i, j;
		for (i = 0; i < 45; i++) {
			for (j = i; j < 45; j++) {
				if (selectLotto[i][1] > selectLotto[j][1]) {
					temp = selectLotto[i][1];
					selectLotto[i][1] = selectLotto[j][1];
					selectLotto[j][1] = temp;

					temp = selectLotto[i][2];
					selectLotto[i][2] = selectLotto[j][2];
					selectLotto[j][2] = temp;
				}

			}
		}
		model = new MyTable(header, selectLotto);
		table.setModel(model);
		table.validate();
		this.updateUI();

	}

	public void cntSort() {
		int temp, i, j;
		for (i = 0; i < 45; i++) {
			for (j = i; j < 45; j++) {
				if (selectLotto[i][2] < selectLotto[j][2]) {
					temp = selectLotto[i][1];
					selectLotto[i][1] = selectLotto[j][1];
					selectLotto[j][1] = temp;

					temp = selectLotto[i][2];
					selectLotto[i][2] = selectLotto[j][2];
					selectLotto[j][2] = temp;
				}

			}
		}

		model = new MyTable(header, selectLotto);
		table.setModel(model);
		table.validate();
		this.updateUI();

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == startButton)
			select();
		if (e.getSource() == numberSort)
			numberSort();
		if (e.getSource() == cntSort)
			cntSort();
	}

}